home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 001-025 / scopedisk11 / memocal / memocal.c < prev    next >
C/C++ Source or Header  |  1995-03-18  |  14KB  |  522 lines

  1. /***************************************************************/
  2. /*            Calendar Program                   */
  3. /*    This program opens a window, reads the system date and   */
  4. /*    displays the current month calendar and date.  The mouse */
  5. /*    may be used to change the date.  Saves date on Exit      */
  6. /***************************************************************/
  7.  
  8. /* modified 30-MAY-88 by Ron Charlton to correctly set initial date. */
  9.  
  10. #include <exec/types.h>
  11. #include <exec/devices.h>
  12. #include <exec/io.h>
  13. #include <exec/libraries.h>
  14. #include <devices/timer.h>
  15. #include <functions.h>
  16. #include <intuition/intuition.h>
  17. #include <graphics/gfxmacros.h>
  18. #include <stdio.h>
  19. #include <ctype.h>
  20. #include <time.h>
  21.  
  22. #define INTUITION_REV 33L
  23. #define GRAPHICS_REV  33L
  24. #define MAXLIN          80
  25.  
  26. char *mn[12] = {"January","February","March","April","May","June","July",
  27.         "August","September","October","November","December"};
  28. short nday[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
  29. char *Curr_Year = "1984";
  30. char *Time = "12:00";
  31. char s[]="Date 12:00 07-Jul-86";
  32. short CC,CR,da,wk,mo,yr,hr,min,sec;
  33. USHORT pot[2];
  34. short matrix[6][7];
  35. static char *lines[6] = {"              ",  /* init lines for calendar*/
  36.              "              ",
  37.              "              ",
  38.              "              ",
  39.              "              ",
  40.              "              ",
  41.             };
  42.  
  43. struct IntuitionBase  *IntuitionBase;
  44. struct GfxBase          *GfxBase;
  45. struct Window          *Win;
  46. struct FileHandle     *Open();
  47. struct Gadget          Cg[8];
  48. struct PropInfo          Cinfo[2];
  49. struct Image          Cimage[2];
  50.  
  51. main(argc,argv)
  52. int  argc;
  53. char *argv[];
  54. {
  55.     struct IntuiMessage *message;
  56.     struct Gadget    *gad;  
  57.     ULONG  class,code, x,y;
  58.     BOOL   Quit,Save,Memo,GadFlag;
  59.     USHORT ID;
  60.     float  real;
  61.     
  62.     short dx,dy,wkdy,maxday;
  63.     VOID  show_month(),Last_Time(),show_day(),FrameIt(),Point_Block();
  64.     VOID  Reset_Date(),Show_Time(),Show_Help(),OpenAll(),Make_Prop_Gadget(),
  65.       Make_BOOL_Gadget(),Get_Hours(),Get_Minutes(),Tag(),Reminders(),
  66.       Set_Time();
  67.     short day_of_week(),set_days(),Get_Month();
  68.     long  Convert();
  69.  
  70.     Last_Time();OpenAll();FrameIt(Win);
  71.     maxday=set_days(mo+1,yr);wkdy=day_of_week(mo,yr);
  72.     show_month(da,mo,yr,maxday,wkdy);
  73.     Show_Time(0);Point_Block(CC,CR);
  74.     if (argc<2) OffGadget(&Cg[7],Win,NULL);
  75.  
  76.     for (Quit=FALSE,Save=TRUE,Memo=FALSE,GadFlag=FALSE;!Quit;) {
  77.     while (!(message=GetMsg(Win->UserPort))) WaitPort(Win->UserPort);
  78.     class = message->Class;    code = message->Code;
  79.     x     = message->MouseX;   y    = message->MouseY;
  80.     gad   = (struct Gadget *)message->IAddress;
  81.     ReplyMsg( message );
  82.     switch ( class ) {
  83.         case CLOSEWINDOW: Quit=TRUE;Save=FALSE;break;
  84.         case GADGETDOWN:  ID=gad->GadgetID;GadFlag=TRUE;
  85.                   if (ID>5) {
  86.                   Quit=TRUE;if (ID==7) Memo=TRUE;
  87.                 } else if (ID>1) {
  88.                       switch(ID) {
  89.                     case 2: yr--;break;
  90.                     case 3: yr++;break;
  91.                     case 4: mo--;if (mo<0) { yr--;mo=11; }
  92.                         if (da>nday[mo]) da=nday[mo];
  93.                         break;
  94.                     case 5: mo++;if (mo>11) { yr++;mo=0; }
  95.                         if (da>nday[mo]) da=nday[mo];
  96.                         break;
  97.                        }
  98.                       maxday=set_days(mo+1,yr);
  99.                       wkdy=day_of_week(mo,yr);
  100.                       Point_Block(CC,CR);
  101.                       show_month(da,mo,yr,maxday,wkdy);
  102.                       Point_Block(CC,CR);
  103.                     }
  104.                    break;
  105.         case MOUSEBUTTONS: if (code==SELECTDOWN && y>47 && y<121) {
  106.             dx=(x-6)/24;dy=(y-50)/10;
  107.             if (dx<0) dx=0;if (dx>6) dx=6;
  108.             if (dy<0) dy=0;if (dy>5) dy=5;
  109.             if (matrix[dy][dx]) {
  110.             Point_Block(CC,CR);Point_Block(dx,dy);
  111.             CC=dx;CR=dy;da=matrix[dy][dx];
  112.               }
  113.             GadFlag=FALSE;
  114.           }
  115.         break;
  116.         case MOUSEMOVE:if (ID<2 && GadFlag && Cinfo[ID].HorizPot!=pot[ID]){
  117.             real=(Cinfo[ID].HorizPot+1.0)/65536.0;
  118.             if (ID==0) hr=real*24; else min=real*60;
  119.             pot[ID]=Cinfo[ID].HorizPot;Show_Time();
  120.          }
  121.       }
  122.       }
  123.     CloseWindow(Win);
  124.     if (yr>1978 && Save) {
  125.     Reset_Date();Set_Time(Convert());Tag();
  126.     if (Memo && argc==2) Reminders(argc,argv);
  127.       }
  128. }
  129.  
  130.  
  131. VOID Tag()        /* Write File with current date to S: */
  132. {
  133.     struct FileHandle    *out;
  134.  
  135.     if ((out=Open("S:now",1006L)) != NULL) {
  136.     Write(out,&s,(long)strlen(s));Close(out);
  137.       } else printf("Can't update S:now !!!\n");
  138. }
  139.  
  140. VOID Reminders(num,arg)        /* Display Reminders */
  141. int num;
  142. char *arg[];
  143. {
  144.     FILE   *in;
  145.     char   line[MAXLIN];
  146.     short  min,max,day,month,time,cnt,sw,a1,a2;
  147.  
  148.     if (da<6) min=(mo-1)*100+(nday[mo-1]+da-5);
  149.         else min=mo*100+da-5;
  150.     if ((da+10)>nday[mo]) max=(mo+1)*100+(da+10-nday[mo]);
  151.         else max=mo*100+da+10;
  152.     if (min<1) min=1;
  153.     if (max>1131) max=1131;sw=1;
  154.     if ((in=fopen(arg[1],"r")) != NULL) {
  155.         while (fgets(line,MAXLIN,in) != NULL) {
  156.         day=line[0]-48;
  157.         if (day<1) day=0;
  158.         if (line[1] != 32) {
  159.             day=day*10+line[1]-48;
  160.             cnt=3;
  161.           } else cnt=2;
  162.         month=Get_Month(line[cnt],line[cnt+1],line[cnt+2])-1;
  163.         time=month*100+day;
  164.         if ((time>=min) && (time<=max)) {
  165.             if (sw) {
  166.             printf("\n");
  167.             printf("%2d %-13sReminders\n",da,mn[mo]);
  168.             printf("-----------------------------------\n");
  169.             sw=0;
  170.               }
  171.             printf(line);
  172.           }
  173.           }
  174.         fclose(in);
  175.       } else printf("%s file not available...\n",arg[1]);
  176.     printf("\n");
  177. }
  178.  
  179. short Get_Month(a,b,c)        /* Convert Alpha Month to Number */
  180. short a,b,c;
  181. {
  182.     short val;
  183.  
  184.     if (a>96) a-=32;
  185.     if (b>96) b-=32;
  186.     if (c>96) c-=32;
  187.     switch (a) {
  188.     case 65: if (b==80) val=4; else val=8;
  189.          break;
  190.     case 68: val=12;break;
  191.     case 70: val=2;break;
  192.     case 74: if (b==65) val=1;
  193.              else if (c==78) val=6; else val=7;
  194.          break;
  195.     case 77: if (c==82) val=3; else val=5;
  196.          break;
  197.     case 78: val=11;break;
  198.     case 79: val=10;break;
  199.     case 83: val=9;break;
  200.     }
  201.     return val;
  202. }
  203.  
  204. VOID OpenAll()        /* Open everything */
  205. {
  206.     struct NewWindow nw;
  207.     USHORT i;
  208.     float  real;
  209.  
  210.     if (!(IntuitionBase=(struct IntuitionBase *)
  211.     OpenLibrary("intuition.library",INTUITION_REV))) {
  212.         printf("Intuition Open failed.\n");
  213.         exit(FALSE);
  214.       }
  215.     if (!(GfxBase=(struct GfxBase *)
  216.     OpenLibrary("graphics.library",GRAPHICS_REV))) {
  217.         printf ("graphics open failed.\n");
  218.         exit(FALSE);
  219.       }
  220.     nw.LeftEdge=232;        nw.TopEdge=12;        nw.Width=178;
  221.     nw.Height=144;        nw.DetailPen=0x00;    nw.BlockPen=0x01;
  222.     nw.Title=(UBYTE *)" Calendar ";
  223.     nw.Flags=ACTIVATE|SMART_REFRESH|WINDOWCLOSE|WINDOWDRAG|
  224.         WINDOWDEPTH|REPORTMOUSE;
  225.     nw.IDCMPFlags=CLOSEWINDOW|MOUSEBUTTONS|MOUSEMOVE|GADGETDOWN|GADGETUP;
  226.     nw.Type=WBENCHSCREEN;    nw.FirstGadget=&Cg[0];    nw.CheckMark=NULL;
  227.     nw.Screen=NULL;        nw.BitMap=NULL;     nw.MinWidth=100;
  228.     nw.MinHeight=60;        nw.MaxWidth=640;    nw.MaxHeight=200;
  229.  
  230.     real=(float)hr/24.0;pot[0]=real*65536+1;
  231.     real=(float)min/60.0;pot[1]=real*65536+1;
  232.  
  233.     Make_Prop_Gadget(&Cg[0],8,120,80,7,&Cinfo[0],&Cimage[0],pot[0]);
  234.     Cg[0].NextGadget=&Cg[1];
  235.     Make_Prop_Gadget(&Cg[1],90,120,80,7,&Cinfo[1],&Cimage[1],pot[1]);
  236.     Cg[1].NextGadget=&Cg[2];
  237.     Make_BOOL_Gadget(&Cg[2],9,14,48,8);Cg[2].NextGadget=&Cg[3];
  238.     Make_BOOL_Gadget(&Cg[3],120,14,48,8);Cg[3].NextGadget=&Cg[4];
  239.     Make_BOOL_Gadget(&Cg[4],9,24,48,8);Cg[4].NextGadget=&Cg[5];
  240.     Make_BOOL_Gadget(&Cg[5],120,24,48,8);Cg[5].NextGadget=&Cg[6];
  241.     Make_BOOL_Gadget(&Cg[6],9,130,76,8);Cg[6].NextGadget=&Cg[7];
  242.     Make_BOOL_Gadget(&Cg[7],93,130,76,8);
  243.     for (i=0;i<8;i++) Cg[i].GadgetID=i;
  244.     Win=(struct Window *)(OpenWindow(&nw));
  245. }
  246.  
  247. VOID Make_Prop_Gadget(g,l,t,w,h,info,image,pot)
  248. struct Gadget *g;
  249. USHORT l,t,w,h;
  250. USHORT pot;
  251. struct Image    *image;
  252. struct PropInfo *info;
  253. {
  254.     info->Flags=AUTOKNOB|FREEHORIZ;
  255.     info->VertPot=0L;        info->HorizPot=(ULONG)pot;
  256.     info->HorizBody=1L;        info->VertBody=1L;
  257.     g->NextGadget=NULL;        g->LeftEdge=l;           g->TopEdge=t;
  258.     g->Width=w;            g->Height=h;
  259.     g->Flags=GADGHCOMP|SELECTED;
  260.     g->Activation=GADGIMMEDIATE|FOLLOWMOUSE|RELVERIFY;
  261.     g->GadgetType=PROPGADGET;    g->GadgetRender=(APTR)image;
  262.     g->SelectRender=NULL;    g->GadgetText=NULL;  g->MutualExclude=NULL;
  263.     g->SpecialInfo=(APTR)info;    g->GadgetID=NULL;    g->UserData=NULL;
  264. }
  265.  
  266. VOID Make_BOOL_Gadget(g,l,t,w,h)
  267. struct Gadget *g;
  268. USHORT    l,t,w,h;
  269. {
  270.     g->NextGadget=NULL;        g->LeftEdge=l;           g->TopEdge=t;
  271.     g->Width=w;            g->Height=h;
  272.     g->Flags=GADGHCOMP;
  273.     g->Activation=GADGIMMEDIATE;
  274.     g->GadgetType=BOOLGADGET;    g->GadgetRender=NULL;
  275.     g->SelectRender=NULL;    g->GadgetText=NULL;  g->MutualExclude=NULL;
  276.     g->SpecialInfo=NULL;    g->GadgetID=NULL;    g->UserData=NULL;
  277. }
  278.  
  279. void Reset_Date()        /* Reset Date String to Current Date */
  280. {
  281.     hr %=24;
  282.     s[5]=(hr/10)+48;
  283.     s[6]=(hr%10)+48;
  284.     s[8]=(min/10)+48;
  285.     s[9]=(min%10)+48;
  286.     s[11]=(da/10)+48;
  287.     s[12]=(da%10)+48;
  288.     s[14]=mn[mo][0];
  289.     s[15]=mn[mo][1];
  290.     s[16]=mn[mo][2];
  291.     s[18]=((yr%100)/10)+48;
  292.     s[19]=(yr%10)+48;
  293. }
  294.  
  295. void Show_Time(x)        /* Display Time */
  296. short x;
  297. {
  298.   short h,i,morn;
  299.  
  300.     if (min>59) min=min-60;
  301.     if (min<0) min=60+min;
  302.     if (hr>24) hr=hr-24;
  303.     if (hr<1) hr=24+hr;
  304.     if (hr>12) h=hr-12; else h=hr;
  305.     Time[0]=(h/10)+48;
  306.     Time[1]=(h%10)+48;
  307.     Time[3]=(min/10)+48;
  308.     Time[4]=(min%10)+48;
  309.     Move(Win->RPort,69L,118L);
  310.     Text(Win->RPort,Time,5L);
  311.     Move(Win->RPort,118L,118L);
  312.     SetAPen(Win->RPort,3L); 
  313.     if ((hr>11) && (hr<24)) Text(Win->RPort,"PM",2L);
  314.       else Text(Win->RPort,"AM",2L);
  315.     SetAPen(Win->RPort,1L);   
  316. }
  317.  
  318. void Point_Block(nx,ny)        /* Draw Cursor Square around Day */
  319. SHORT nx,ny;
  320. {
  321.    LONG x,y;
  322.  
  323.     x=nx*24+7;y=ny*10+58;
  324.     SetDrMd(Win->RPort,COMPLEMENT);
  325.     Move(Win->RPort,x+1,y);
  326.     Draw(Win->RPort,x+19,y);
  327.     Draw(Win->RPort,x+19,y-10);
  328.     Draw(Win->RPort,x,y-10);
  329.     Draw(Win->RPort,x,y);
  330.     SetDrMd(Win->RPort,JAM2);
  331. }
  332.  
  333. void FrameIt(w)            /* Draw Frame for Calendar Window */
  334. struct Window *w;
  335. {   
  336.     short i;
  337.  
  338.     SetDrMd(Win->RPort,JAM2);
  339.     SetAPen(Win->RPort,3L);
  340.     Move(w->RPort,5L,12L);
  341.     Draw(w->RPort,172L,12L);
  342.     Draw(w->RPort,172L,141L);
  343.     Draw(w->RPort,5L,141L);
  344.     Draw(w->RPort,5L,12L);
  345.     Move(w->RPort,5L,35L);
  346.     Draw(w->RPort,172L,35L);
  347.     Move(w->RPort,5L,109L);
  348.     Draw(w->RPort,172L,109L);
  349.     Move(w->RPort,5L,128L);
  350.     Draw(w->RPort,172L,128L);
  351.     Move(w->RPort,89L,128L);
  352.     Draw(w->RPort,89L,141L);
  353.     SetAPen(Win->RPort,1L);
  354.     RectFill(Win->RPort,9L,130L,85L,139L);
  355.     RectFill(Win->RPort,93L,130L,168L,139L);
  356.     Move(Win->RPort,22L,118L);Text(Win->RPort,"Time",4L);
  357.     SetAPen(Win->RPort,2L);
  358.     Move(Win->RPort,9L,32L);
  359.     Text(Win->RPort," <<<<        >>>> ",20L);
  360.     Move(Win->RPort,9L,22L);
  361.     Text(Win->RPort," <<<<        >>>> ",20L);
  362.     SetBPen(Win->RPort,1L);
  363.     Move(Win->RPort,15L,137L);Text(Win->RPort,"Set Date",8L);
  364.     Move(Win->RPort,98L,137L);Text(Win->RPort,"Memo/Set",8L);
  365.     SetBPen(Win->RPort,0L);
  366. }
  367.  
  368. void Last_Time()        /* Read the System Date and Time */
  369. {
  370.  
  371.     struct tm *localtime(),*tstruct;
  372.     long   tloc,time(),t;
  373.     short  i;
  374.  
  375.     tloc=time();
  376.     tstruct=localtime(&tloc);
  377.     mo=tstruct->tm_mon;
  378.     yr=tstruct->tm_year+1900;
  379.     da=tstruct->tm_yday;
  380.     wk=tstruct->tm_wday;
  381.     hr=tstruct->tm_hour;
  382.     min=tstruct->tm_min;
  383.     sec=tstruct->tm_sec;
  384.     for (i=0;i<mo;i++) da -= set_days(i+1,yr);
  385.     if (da<1) {
  386.     mo-=1;da=set_days(mo+1,yr);
  387.       } 
  388. }
  389.  
  390. short day_of_week(month,yr)    /* Calculate the Day of the Week */
  391. short month,yr;
  392. {
  393.     float r;
  394.     short i,j,k,l;
  395.  
  396.     r=365.0*yr+1+31.0*month;month=month+1;
  397.     if (month>2) {
  398.         i=0.4*month+2.3;j=yr/4;k=yr/100;l=0.75*(k+1);
  399.         r=r-i+j-l;
  400.     } else {
  401.         i=(yr-1)/4;j=(yr-1)/100;k=0.75*(j+1);
  402.         r=r+i-k;
  403.       }
  404.     r=r-273750.0;
  405.     j=(-1.0*r)/7.0;i=r+j*7.0;
  406.     if(i==7) i=0;
  407.     return i;
  408. }
  409.  
  410. void show_month(da,month,yr,mxday,wkday)  /* Display Day, Month, Year */
  411. short da,month,yr,mxday,wkday;
  412. {
  413.     short cnt,row,i,j,x,y;
  414.  
  415.     for (i=0;i<6;i++) {
  416.       for (j=0;j<7;j++) {
  417.         matrix[i][j]=0;
  418.         lines[i][j*3]=32;
  419.         lines[i][j*3+1]=32;
  420.        }
  421.      }
  422.     Move(Win->RPort,49L,32L);
  423.     Text(Win->RPort,"       ",10L);
  424.     SetAPen(Win->RPort,1L);
  425.     i=(178-strlen(mn[month])*8)/2;
  426.     Curr_Year[0]=(yr/1000)+48;
  427.     Curr_Year[1]=((yr%1000)/100)+48;
  428.     Curr_Year[2]=((yr%100)/10)+48;
  429.     Curr_Year[3]=yr%10+48;
  430.     Move(Win->RPort,73L,22L);
  431.     Text(Win->RPort,Curr_Year,4L);
  432.     Move(Win->RPort,(long)i,32L);
  433.     Text(Win->RPort,mn[month],(long)strlen(mn[month]));
  434.     Move(Win->RPort,9L,46L);
  435.     SetAPen(Win->RPort,3L);
  436.     Text(Win->RPort," S  M    T  W  T  F  S",20L);
  437.     cnt=wkday;row=0;
  438.     for (i=1;i<(mxday+1);i++) {
  439.         if (i==da) {
  440.         CR=row;CC=cnt;
  441.         }
  442.         if (i>9) lines[row][cnt*3]=(i/10)+48;
  443.         else lines[row][cnt*3]=32;
  444.         lines[row][cnt*3+1]=(i%10)+48;
  445.         matrix[row][cnt]=i;    
  446.         cnt++;
  447.         if (cnt==7 && i<mxday) {
  448.         cnt=0;row++;
  449.         }
  450.     }
  451.     SetAPen(Win->RPort,1L);
  452.     for (i=0;i<6;i++) {
  453.         Move(Win->RPort,9L,(long)(i*10+56));
  454.         Text(Win->RPort,lines[i],20L);
  455.     }
  456. }
  457.  
  458. short set_days(month,yr)    /* Check for Leap Year */
  459. short month,yr;
  460. {
  461.    short norm;
  462.  
  463.     if (month==1 || month==3 || month==5 || month==7 || 
  464.                 month==8 || month==10 || month==12)
  465.         return 31;
  466.       else if (month==4 || month==6 || month==9 || month==11)
  467.           return 30;
  468.         else {
  469.         norm = 1;
  470.         if ((yr % 4) == 0) norm = 0;
  471.         if ((yr % 100) == 0) norm = 1;
  472.         if ((yr % 400) == 0) norm = 0;
  473.         if (norm) return 28;
  474.             else return 29;
  475.           }
  476. }
  477.  
  478. VOID  Set_Time(secs)     /* Set System Clock */
  479. long secs;
  480. {
  481.     struct timerequest *time_req;
  482.     struct timerequest *CreateExtIO();
  483.     struct MsgPort     *timer_port;
  484.     long   error;
  485.          
  486.     timer_port = CreatePort( 0L, 0L );
  487.     if ( ! timer_port ) exit( 1L );
  488.     time_req=CreateExtIO(timer_port,(long)sizeof(struct timerequest));
  489.     if (time_req==NULL) exit(1L);
  490.     error =  OpenDevice (TIMERNAME,UNIT_MICROHZ,time_req,0L);
  491.     if( error ) {
  492.     DeleteExtIO(time_req,(long)sizeof(struct timerequest));
  493.     DeletePort( timer_port );
  494.     exit( 2L );
  495.       }
  496.     time_req -> tr_node.io_Message . mn_ReplyPort = timer_port;
  497.     time_req -> tr_node.io_Command = TR_SETSYSTIME;
  498.     time_req -> tr_time.tv_secs    = secs;
  499.     time_req -> tr_time.tv_micro   = 0L;
  500.     DoIO( time_req );
  501.     CloseDevice( time_req );
  502.     DeleteExtIO(time_req,(long)sizeof(struct timerequest)); 
  503.     DeletePort( timer_port );
  504.  }
  505.  
  506. long Convert()        /* Convert Time to Seconds */
  507. {
  508.     long secs,years,days;
  509.     short i;
  510.  
  511.     years=yr-1978;
  512.     if (years<0) return(0L);
  513.     for (i=0,days=0;i<mo;i++) days+=nday[i];
  514.     days+=da-1;
  515.     if (yr>1979) {
  516.     days+=((yr-1980)/4)+1;
  517.     if (((yr-1980)%4)==0 && mo<2) days--;
  518.       }
  519.     secs=(years*365+days)*86400+(long)hr*3600+min*60+sec;
  520.     return(secs);
  521.